//
//  InsideAnimationEffectShowManager.m
//  PruchaseCarAnimation
//
//  Created by iOS的 on 2021/12/17.
//  Copyright © 2020 Moitnea. All rights reserved.
//
#import "InsideAnimationEffectShowManager.h"
#define   APP_WIDTH        [UIScreen mainScreen].bounds.size.width
@interface InsideAnimationEffectShowManager()<CAAnimationDelegate,CAAnimationDelegate>

@end

@implementation InsideAnimationEffectShowManager
#pragma mark - instancetype
+ (instancetype)shareTool
{
    return [[InsideAnimationEffectShowManager alloc]init];
}
#pragma public function
- (void)startAnimationandView:(UIView *)view
                         rect:(CGRect)rect
                  finisnPoint:(CGPoint)finishPoint
                  finishBlock:(animationFinisnBlock)completion showView:(UIView *)showview
{
    //图层
    _layer = [CALayer layer];
    _layer.contents = view.layer.contents;
    _layer.contentsGravity = kCAGravityResizeAspectFill;
    _layer.bounds = rect;
    [showview.layer addSublayer:_layer];
    _layer.position = CGPointMake(rect.origin.x+(rect.size.width/2), rect.origin.y+(rect.size.height/2)); //a 点
    /// 路径
    [self createAnimationwithRect:rect finishPoint:finishPoint];
    /// 回调
    if (completion) {
        _animationFinisnBlock = completion;
    }
}

- (void)startVerticalAnimationandView:(UIView *)view
                         rect:(CGRect)rect
                  finisnPoint:(CGPoint)finishPoint
                  finishBlock:(animationFinisnBlock)completion showView:(UIView *)showview
{
    //图层
    _layer = [CALayer layer];
    _layer.contents = view.layer.contents;
    _layer.contentsGravity = kCAGravityResizeAspectFill;
    _layer.bounds = rect;
    [showview.layer addSublayer:_layer];
    _layer.position = CGPointMake(rect.origin.x+(rect.size.width/2), rect.origin.y+(rect.size.height/2)); //a 点
    /// 路径
    [self createVerticalAnimationwithRect:rect finishPoint:finishPoint];
    /// 回调
    if (completion) {
        _animationFinisnBlock = completion;
    }
}

#pragma mark - private function
/// 创建动画
- (void)createAnimationwithRect:(CGRect)rect
                    finishPoint:(CGPoint)finishPoint {
    /// 路径动画
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:_layer.position];
    [path addQuadCurveToPoint:finishPoint controlPoint:CGPointMake(APP_WIDTH/2, rect.origin.y)];
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.path = path.CGPath;
    /// 旋转动画
    CABasicAnimation *rotateAnimation   = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    rotateAnimation.removedOnCompletion = YES;
    rotateAnimation.fromValue = [NSNumber numberWithFloat:1];
    rotateAnimation.toValue   = [NSNumber numberWithFloat:0.6];
    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    /// 添加动画动画组合
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[pathAnimation,rotateAnimation];
    groups.duration = 0.5f;
    groups.removedOnCompletion=NO;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    [_layer addAnimation:groups forKey:@"group"];
}
- (void)createVerticalAnimationwithRect:(CGRect)rect
                    finishPoint:(CGPoint)finishPoint {
    /// 路径动画
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:_layer.position];
    [path addQuadCurveToPoint:finishPoint controlPoint:CGPointMake(rect.origin.y, rect.origin.y)];
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.path = path.CGPath;
    /// 缩放动画
    CABasicAnimation *rotateAnimation   = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    rotateAnimation.removedOnCompletion = YES;
    rotateAnimation.fromValue = [NSNumber numberWithFloat:1];
    rotateAnimation.toValue   = [NSNumber numberWithFloat:0.4];
    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    
    CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    scaleAnimation.values = values;
    scaleAnimation.duration = 0.25;
    scaleAnimation.cumulative = NO;
    scaleAnimation.repeatCount = 1;
    
    /// 添加动画动画组合
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[rotateAnimation];
    groups.duration = 0.2f;
    groups.removedOnCompletion=NO;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    [_layer addAnimation:groups forKey:@"group"];
    
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (anim == [_layer animationForKey:@"group"]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self->_layer removeFromSuperlayer];
            self->_layer = nil;
        });

    }else if (anim == [_layer animationForKey:@"myScale"]) {
   
    }
    if (_animationFinisnBlock) {
        _animationFinisnBlock(YES);
    }
}
-(void)labelScaleAnimation:(UILabel *)label{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@0.2,@2.2,@1.0];
    animation.duration = 0.5;
    animation.calculationMode = kCAAnimationPaced;
    [label.layer addAnimation:animation forKey:nil];
}
- (void)scaleSmallAnimation:(UIView *)scaleView duration:(double)duration finishBlock:(animationFinisnBlock)completion
{
        if (completion) {
               _animationFinisnBlock = completion;
        }
        CAGradientLayer *contentLayer = (CAGradientLayer *)scaleView.layer;
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
        scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)];
        scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1)];
        scaleAnimation.duration = duration;
        scaleAnimation.cumulative = NO;
        scaleAnimation.repeatCount = 1;
        scaleAnimation.delegate = self;
        [scaleAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
        [contentLayer addAnimation:scaleAnimation forKey:@"myScale"];
    
}
   
+ (void)scaleBigAnimation:(UIView *)scaleView duration:(double)duration{
        CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        scaleAnimation.values = values;
        scaleAnimation.duration = duration;
        scaleAnimation.cumulative = NO;
        scaleAnimation.repeatCount = 1;
        [scaleView.layer addAnimation:scaleAnimation forKey:nil];
}

+ (void)scaleAnimation:(UIView *)scaleView duration:(double)duration  repeatCount:(float)repeatCount bigSize:(CGFloat)bigSize{
        CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        pulseAnimation.duration = duration;
        pulseAnimation.toValue = [NSNumber numberWithFloat:bigSize];
        pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pulseAnimation.autoreverses = YES;
        pulseAnimation.repeatCount = repeatCount;
        [scaleView.layer addAnimation:pulseAnimation forKey:@"transform.scale"];
}
+ (void)scaleAnimation:(UIView *)scaleView duration:(double)duration  repeatCount:(float)repeatCount
{
    [self scaleAnimation:scaleView duration:duration repeatCount:repeatCount bigSize:1.3];
}





+ (void)scaleBackviewBigAnimation:(UIView *)scaleView duration:(double)duration{
        CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(2, 2, 1.0)]];
        scaleAnimation.values = values;
        scaleAnimation.duration = duration;
        scaleAnimation.cumulative = NO;
        scaleAnimation.repeatCount = 1;
        [scaleView.layer addAnimation:scaleAnimation forKey:nil];
}
@end
